var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        link: '/pages/index/index',
        url: '/images/zhiwei1.jpg'
      }, {
        link: '/pages/news/index',
        url: '/images/zhiwei2.jpg'
      }
    ],
    isDefTap: true,
    hasUserInfo: false,
    matchPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    positionPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    currentTab: 0,
    banner: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    isHaveResume: null,
    nonet: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.userInfo != null) {
      that.setData({ hasUserInfo: true });
    }
    this.resetData(true);
  },
  //职位详情
  positionDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    // 切换页面
    wx.navigateTo({
      url: '../position-detail/position-detail?pid=' + id
    });
  },

  resetDataForPosition: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    util.post(url, { page: now_page_no, size: 5 }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载中......',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'positionPage.loading': false });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({ 'positionPage.noMore': false });
        //下拉刷新 此处停止刷新效果  重置now_page_no为1
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      that.setData({ 'positionPage.pageNum': res.data.pageNum });
      that.setData({ 'positionPage.data': data, 'positionPage.now_page_no': now_page_no });
    });
  },

  resetDataForMatch: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    var unionid = wx.getStorageSync('unionid');
    //tags=1:top 5 position
    util.post(url, { unionid: unionid, page: now_page_no, size: 20 ,tags:1}, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'matchPage.loading': false });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({ 'matchPage.noMore': false });
        //下拉刷新 此处停止刷新效果
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
      }
      that.setData({ 'matchPage.pageNum': res.data.pageNum });
      that.setData({ 'matchPage.data': data, 'matchPage.now_page_no': now_page_no });
    });
  },
  resetData: function (isOnLoad, isappend, now_page_no) {
    var isappend = isappend == undefined ? false : true;
    var now_page_no = now_page_no == undefined ? 1 : now_page_no;
   
      //如果是onload函数中执行此函数，那么resetDataForPosition和resetDataForMatch都要执行
      this.resetDataForMatch(app.api.companyTagsPosi, now_page_no, isappend);
    

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady:function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        var networkType = res.networkType
        if (networkType == "none") {
          that.setData({
            nonet: false,
            gobtn: true,
            perfectbtn: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.resetData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currObj = null;
    if (this.data.currentTab == 0) {
      //最新职位
      currObj = this.data.positionPage;
    } else {
      //精准匹配
      currObj = this.data.matchPage;
    }

    if (currObj.loading) return;
    if (currObj.now_page_no >= currObj.pageNum) {

      if (this.data.currentTab == 0) {
        //最新职位
        this.setData({ 'positionPage.noMore': true });
      } else {
        //精准匹配
        this.setData({ 'matchPage.noMore': true });
      }

    } else {
      this.resetData(false, 1, currObj.now_page_no);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})